package com.cheng.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cheng.admin.model.Template;
import com.cheng.admin.service.TemplateService;
import com.cheng.common.domain.BaseSearchVO;
import com.cheng.common.domain.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 信封模板 前端控制器
 * </p>
 *
 * @author 起凡
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/template")
public class TemplateController {
    @Autowired
    TemplateService templateService;

    @GetMapping("get")
    public R<Template> get(@RequestParam Long id) {
        return R.ok(templateService.getById(id));
    }

    @PostMapping("save")
    public R<Boolean> save(@RequestBody @Validated Template template) {
        templateService.saveOrUpdate(template);
        return R.ok(true);
    }

    @GetMapping("list")
    public R<PageInfo<Template>> list(BaseSearchVO searchVO) {
        PageHelper.startPage(searchVO.getPageNum(), searchVO.getPageSize());
        LambdaQueryWrapper<Template> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(searchVO.getKeyword())) {
            queryWrapper.like(Template::getTitle, searchVO.getKeyword());
        }
        queryWrapper.orderByDesc(Template::getCreateTime);
        PageInfo<Template> pageInfo = new PageInfo<>(templateService.list(queryWrapper));
        return R.ok(pageInfo);
    }

    @GetMapping("delete")
    public R<Boolean> delete(Long id) {
        return R.ok(templateService.removeById(id));
    }
}

